import cv2
import numpy as np
# 读取目标图像和模板图像
target_img = cv2.imread('/dev/image\duanzao_success_mod.png')
template_img = cv2.imread('/dev/image\duanzao_fail_tmp.png')
# 转换为灰度图像（模板匹配通常在灰度图像上进行）
target_gray = cv2.cvtColor(target_img, cv2.COLOR_BGR2GRAY)
template_gray = cv2.cvtColor(template_img, cv2.COLOR_BGR2GRAY)
# 执行模板匹配
result = cv2.matchTemplate(target_gray, template_gray, cv2.TM_CCOEFF_NORMED)
# 设置阈值
print(result)
threshold = 0.8
loc = np.where(result >= threshold)
print(loc)
# 绘制匹配区域
for pt in zip(*loc[::-1]):
    cv2.rectangle(target_img, pt, (pt[0] + template_img.shape[1], pt[1] + template_img.shape[0]), (0, 255, 0), 2)
# 显示结果
cv2.imshow('Detected', target_img)
cv2.waitKey(0)
cv2.destroyAllWindows()